package edu.henu.personnel.controller;

import com.github.pagehelper.PageInfo;
import edu.henu.personnel.entity.Talent;
import edu.henu.personnel.entity.Teacher;
import edu.henu.personnel.service.talent.TalentService;
import edu.henu.personnel.service.teacher.TeacherService;
import edu.henu.personnel.utils.DateUtil;
import edu.henu.personnel.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 优秀教师控制器
 * @author: zhuyang
 * @create: 2019-05-13 17:51
 */

@Slf4j
@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TalentService talentService;

    @GetMapping("/teachers/{pageNum}")
    public String list(@PathVariable("pageNum") Integer pageNum,
                       Model model) {
        PageInfo<Teacher> teacherList = teacherService.getAllTeacherInfo(pageNum);
        model.addAttribute("teachers", teacherList);
        return "teacher/list";
    }

    @GetMapping("/teacher")
    public String toAddTeacher(Model model) {
        List<Talent> talentList = talentService.getTalentList();
        model.addAttribute("talentList", talentList);
        return "teacher/add";
    }

    @PostMapping("/teacher")
    public String addTeacher(Teacher teacher, @RequestParam("file") MultipartFile file) {

        // 为teacher设置发布日期
        String fileDate = DateUtil.getStoreDate();
        teacher.setTdate(fileDate);

        // 为teacher初始化浏览量
        teacher.setTbrowses(0);

        try {
            String target = FileUtil.imgUpload(file, FileUtil.imgPath);
            teacher.setTphoto(target);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int result = teacherService.addTeacherInfo(teacher);
        return "redirect:/teachers/1";
    }

    @GetMapping("/teacher/{tid}")
    public String toEditPage(@PathVariable("tid") String tid, Model model) {
        Teacher teacher = teacherService.getTeacherById(tid);
        model.addAttribute("teacher", teacher);

        List<Talent> talentList = talentService.getTalentList();
        model.addAttribute("talentList", talentList);

        return "teacher/modify";
    }

    @PutMapping("/teacher")
    public String modifyTeacher(Teacher teacher, Model model) {
        Teacher tempTeacher = teacherService.getTeacherById(teacher.getTid());

        tempTeacher.setTname(teacher.getTname());
        tempTeacher.setTtid(teacher.getTtid());
        tempTeacher.setTsource(teacher.getTsource());
        tempTeacher.setTintro(teacher.getTintro());
        int result = teacherService.updateTeacherInfo(tempTeacher);
        return "redirect:/teachers/1";
    }

    @DeleteMapping("/teacher/{tid}")
    public String deleteTeacher(@PathVariable("tid") String tid, Model model) {
        int result = teacherService.deleteTeacherById(tid);
        model.addAttribute("deleteTeacherResult", result);
        return "redirect:/teachers/1";
    }

    // 判断教师的tid是否存在，存在不允许添加
    @PostMapping("/judgeTid")
    @ResponseBody
    public String judgeTid(@RequestParam("tid") String tid) {
        if (StringUtils.isBlank(tid)) {
            return "ERROR";
        } else {
            Teacher teacher = teacherService.getTeacherById(tid);
            if (teacher != null) {
                return "FALSE";
            } else {
                return "OK";
            }
        }
    }

}
